import axios from "axios";

// 创建axios实例
const initAxios = axios.create({
  timeout: 1800000, 
  baseURL: "http://localhost:8080"
});

// 请求拦截器
initAxios.interceptors.request.use((config) => {
  if (sessionStorage.getItem("tokenStr")) {
    config.headers['Authorization'] = sessionStorage.getItem("tokenStr");
    config.headers['Content-Type'] = 'application/json';
  }
  return config;
}, (error) => {
  return Promise.reject(error);
});

// 响应拦截器
initAxios.interceptors.response.use((response) => {
  if (response.status === 200) {
    return response.data;
  } else {
    return Promise.reject(new Error('响应错误'));
  }
}, (error) => {
  return Promise.reject(error);
});

// 导出实例
export default initAxios;
